package cn.k.service.impl;

import cn.k.domain.Appointment;
import cn.k.domain.Consumption;
import cn.k.domain.Employee;
import cn.k.mapper.ConsumptionMapper;
import cn.k.qo.ConsumptionQueryObject;
import cn.k.service.IAppointmentService;
import cn.k.service.IConsumptionService;
import cn.k.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Service
public class ConsumptionServiceImpl implements IConsumptionService {
    @Autowired
    private ConsumptionMapper consumptionMapper;

    @Autowired
    private IAppointmentService appointmentService;

    @Override
    @Transactional
    public void insert(Consumption consumption) {
        //1.流水号
        String date = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        //随机数
        int random=new Random().nextInt(90000)+10000;
        consumption.setCno(date+random);
        //2.创建时间
        consumption.setCreateTime(new Date());
        //3.状态
        consumption.setStatus(Consumption.STATUS_TYPE_CONSUME);
        //4.创建人 ,需要登录才会都session
        Employee employee = UserContext.getEmployee();
        consumption.setCreateUserId(employee.getId());

        consumptionMapper.insert(consumption);
    }

    @Override
    public void delete(Long id) {
        consumptionMapper.deleteByPrimaryKey(id);

    }

    @Override
    @Transactional
    public void update(Consumption consumption) {
        //Employee employee = UserContext.getEmployee();
        //consumption.setCreateUserId(employee.getId());

        consumptionMapper.updateByPrimaryKey(consumption);

    }

    @Override
    public Consumption get(Long id) {
        Consumption consumption = consumptionMapper.selectByPrimaryKey(id);
        return consumption;
    }

    @Override
    public List<Consumption> selectAll() {
        List<Consumption> consumptions = consumptionMapper.selectAll();
        return consumptions;
    }


    @Override
    public List<Consumption> selectForList(ConsumptionQueryObject qo) {
        List<Consumption> consumptions = consumptionMapper.selectForList(qo);
        return consumptions;
    }

    @Override
    public PageInfo<Consumption> query(ConsumptionQueryObject qo) {
        //告诉插件我们查询第几页,每页多少条
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        //查询当前页数据
        List<Consumption> consumptions = consumptionMapper.selectForList(qo);
        return new PageInfo<>(consumptions);
    }

    @Override
    public Long save(Long aid) {
        //1.得到预约状态 :消费中,设置消费中状态
        appointmentService.updateStatus(aid, Appointment.STATUS_TYPE_CONSUME);
        //新增雨夜的关联结算单
        Consumption consumption = new Consumption();
        Appointment appointment = appointmentService.get(aid);
        //创建用户,时间
        consumption.setCreateUserId(UserContext.getEmployee().getId());
        consumption.setCreateTime(new Date());
        //设置消费单中的状态,待结算
        consumption.setStatus(Consumption.STATUS_TYPE_CONSUME);
        //设置流水号
        String date = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        int random=new Random().nextInt(90000)+10000;
        consumption.setCno(date+random);

        //关联预约单流水号
        consumption.setAppointmentAno(appointment.getAno());
        //预约门店
        consumption.setBusiness(appointment.getBusiness());
        //客户名称与电话
        consumption.setCustomerName(appointment.getContactName());
        consumption.setCustomerTel(appointment.getContactTel());
        //备注
        consumption.setInfo(appointment.getInfo());

        consumptionMapper.insert(consumption);

        return consumption.getId();
    }

    @Override
    public void updateStatus(Long id, int status) {
        consumptionMapper.updateStatus(id,status);
    }




}
